package com.java110.mall.acct.bmo.impl;

import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountDetailDto;
import com.java110.dto.account.AccountDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.IAccountDetailInnerServiceSMO;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.acct.bmo.IGetAccountBMO;
import com.java110.po.account.AccountPo;
import com.java110.utils.lock.DistributedLock;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getAccountBMOImpl")
public class GetAccountBMOImpl implements IGetAccountBMO {

    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;

    @Autowired
    private IAccountDetailInnerServiceSMO accountDetailInnerServiceSMOImpl;


    //键(积分账户最大使用积分)
    public static final String MAXIMUM_NUMBER = "MAXIMUM_NUMBER";

    //键(积分账户抵扣比例)
    public static final String DEDUCTION_PROPORTION = "DEDUCTION_PROPORTION";

    @Autowired
    //private IUserInnerServiceSMO userInnerServiceSMOImpl;
    private IUserV1InnerServiceSMO userInnerServiceSMOImpl;

    /**
     * @param accountDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(AccountDto accountDto) {


        int count = accountInnerServiceSMOImpl.queryAccountsCount(accountDto);

        List<AccountDto> accountDtos = null;
        if (count > 0) {
            accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        } else {
            accountDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) accountDto.getRow()), count, accountDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    @Override
    public ResponseEntity<String> getDetail(AccountDetailDto accountDetailDto) {
        int count = accountDetailInnerServiceSMOImpl.queryAccountDetailsCount(accountDetailDto);

        List<AccountDetailDto> accountDetailDtos = null;
        if (count > 0) {
            accountDetailDtos = accountDetailInnerServiceSMOImpl.queryAccountDetails(accountDetailDto);
        } else {
            accountDetailDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) accountDetailDto.getRow()), count, accountDetailDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * 查询业主账号
     *
     * @param accountDto
     * @return
     */
    @Override
    public ResponseEntity<String> queryOwnerAccount(AccountDto accountDto) {

        List<AccountDto> accountDtos = null;
        int count = 0;


        count = accountInnerServiceSMOImpl.queryAccountsCount(accountDto);
        if (count > 0) {
            accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        } else {
            accountDtos = new ArrayList<>();
        }


        if (accountDtos == null || accountDtos.size() < 1) {
            //添加 账户
            accountDtos = addAccountDto(accountDto);

        }
        //积分账户最大使用积分


        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) accountDto.getRow()), count, accountDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }


    private List<AccountDto> addAccountDto(AccountDto accountDto) {
        // todo  查询账户名称 这里如果存在业主则业主名称 不是业主 则 填写用户名称，如果用户都没有 则返回空

        //开始锁代码
//        String requestId = DistributedLock.getLockUUID();
//        String key = this.getClass().getSimpleName() + "AddCountDto" + ownerDto.getOwnerId();
//        try {
//            DistributedLock.waitGetDistributedLock(key, requestId);
//
//            AccountPo accountPo = new AccountPo();
//            accountPo.setAmount("0");
//            accountPo.setAcctId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_acctId));
//            accountPo.setObjId(ownerDto.getOwnerId());
//            accountPo.setObjType(AccountDto.OBJ_TYPE_PERSON);
//            accountPo.setAcctType(AccountDto.ACCT_TYPE_CASH);
//            accountPo.setAcctName(acctName);
//            accountPo.setPartId(ownerDto.getCommunityId());
//            accountPo.setLink(ownerDto.getLink());
//            accountInnerServiceSMOImpl.saveAccount(accountPo);
//            List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
//            return accountDtos;
//        } finally {
//            DistributedLock.releaseDistributedLock(requestId, key);
//        }
        return null;
    }


}
